$(document).ready(function () {
    //input标签获取焦点隐藏placeholder提示，失去焦点展示提示
    input_placehoder_status();
    function input_placehoder_status() {
        var text = '';
        $('.define-placeholder ').on('focus',function () {
            text = $(this).attr('placeholder');
            $(this).attr('placeholder','');
        }).on('blur',function () {
            $(this).attr('placeholder',text);
        });
    }

    //动画  抽屉效果
    animation_list();
    function animation_list() {
        $('.define-list-nav').on('click',function () {
            var list = $(this).parents('.container-fluid').find('.define-list');
            var animation = $(this).parents('.container-fluid').find('.define-drawer-animation');
            if(animation.hasClass('define-close-active')){
                animation.removeClass('define-close-active');
                animation.addClass('define-open-active');
            }else {
                animation.removeClass('define-open-active');
                animation.addClass('define-close-active');
            }
            list.animate({
                'height':'toggle'
            },500);
        });
    }

    //选择器
    //选择 性别
    selecter_sex();
    function selecter_sex() {
        var data = [
            {
                value: '001',
                text: '男'
            },
            {
                value: '002',
                text: '女'
            }
        ];
        var btn = document.getElementById('picker-sex');
        btn.addEventListener('tap',function () {
            var self = this;
            var picker = new mui.PopPicker({
                layer:1 //三级联动
            });
            picker.setData(data);
            picker.show(function (data) {
                $(self).find('span').text(data[0].text);
                //表单赋值
                $('#post_form').find('input[name="hos_people_sex"]').val(data[0].text);//投诉人 性别
                picker.dispose();//释放组件
            });
        })
    }

    //选择投诉人所在科室
    selecter_complain_department();
    function selecter_complain_department() {
        var data = [
            {
                value: '001',
                text: '呼吸内科'
            },
            {
                value: '002',
                text: '内分泌科'
            },
            {
                value: '003',
                text: '神经内科'
            },
            {
                value: '004',
                text: '消化内科'
            },
            {
                value: '005',
                text: '血液病研究所'
            },
            {
                value: '006',
                text: '风湿免疫科'
            },
            {
                value: '007',
                text: '泌尿外科'
            },
            {
                value: '008',
                text: '医疗美容科'
            },
            {
                value: '009',
                text: '产科'
            },
            {
                value: '010',
                text: '儿科'
            },
            {
                value: '011',
                text: '眼科'
            },
            {
                value: '012',
                text: '耳鼻喉科'
            },
            {
                value: '013',
                text: '放射科'
            },
            {
                value: '014',
                text: '超声诊断室'
            },
            {
                value: '015',
                text: '输血科'
            }
        ];
        var picker_complain_department = document.getElementById('picker-complain-department');
        picker_complain_department.addEventListener('tap',function () {
            //初始化;
            var picker = new mui.PopPicker({
                layer: 1 //三级联动
            });
            picker.setData(data);
            var self = this;
            picker.show(function (data) {
                var text = '';
                for (p in data){
                    text += data[p].text;
                }
                $(self).find('span').text(text);
                //表单赋值
                $('#post_form').find('input[name="hos_by_people_admin"]').val(text);//被投诉人所在科室
                picker.dispose();//释放组件
            });
        });
    }

    //选择投诉类型
    selecter_complain_status();
    function selecter_complain_status() {
        var data = [
            {
                value: '001',
                text: '非正常状态值岗'
            },
            {
                value: '002',
                text: '丢失重要检验标本'
            },
            {
                value: '003',
                text: '口头医嘱传递错误'
            },
            {
                value: '004',
                text: '错误诊断'
            },
            {
                value: '005',
                text: '数据报告错误'
            },
            {
                value: '006',
                text: '治疗方案错误'
            },
            {
                value: '007',
                text: '药瓶名称开具错误'
            },
            {
                value: '008',
                text: '输血导致感染'
            },
            {
                value: '009',
                text: '输血反应处置不当'
            },
            {
                value: '010',
                text: '麻醉药物剂量错误'
            },
            {
                value: '011',
                text: '抗生素合理应用缺陷'
            },
            {
                value: '012',
                text: '病情监护不当'
            },
            {
                value: '013',
                text: '生活护理不当'
            },
            {
                value: '014',
                text: '医疗器械资质缺陷'
            },
            {
                value: '015',
                text: '医疗器械来源途径不正规'
            }
        ];
        var picker_complain_status = document.getElementById('picker-complain-status');
        picker_complain_status.addEventListener('tap',function () {
            //初始化;
            var picker = new mui.PopPicker({
                layer: 1 //三级联动
            });
            picker.setData(data);
            var self = this;
            picker.show(function (data) {
                var text = '';
                for (p in data){
                    text += data[p].text;
                }
                $(self).find('span').text(text);
                $('#post_form').find('input[name="hos_by_people_status"]').val(text);//被投诉人所在科室
                picker.dispose();//释放组件
            });
        });
    }

    //图片上传  最多三张
    upload_pictures();
    function upload_pictures() {
        $('.define-add-img').on('click',function () {
            returnFileBtn($(this).next().find('input[type="file"]'));
            previewImg($(this).next().find('input[type="file"]'),$(this));
        });
    }

    //返回点击原始input文件夹按钮
    function returnFileBtn($input) {
        return $input.click();
    }

    //选定需要上传的文件之后  展示在窗口  预览即将上传的图片
    function previewImg($input,$this) {
        $input.change(function () {
            var objUrl = getObjectURL(this.files[0]) ;//获取文件url
            //如果不是img文件 ，提示不能选择此类文件
            if (this.files[0].type !== 'image/jpeg' && this.files[0].type !== 'image/png' && this.files[0].type !== 'image/gif') {
                alert('不是有效的图片文件!');
                $this.attr('src', '/statics/images/conplain_add.png');//赋值控制给img
                return;
            }
            //获取到url 赋值img的url  并展示
            if (objUrl) {
                $this.attr('src', objUrl);//赋值控制给img
                uploadImgToServers($(this),$this);
            }
        });
    }

    //获取文件的url
    function getObjectURL(file) {
        var url = null ;
        if (window.createObjectURL != undefined) {
            url = window.createObjectURL(file) ;// basic
        }
        else if (window.URL != undefined) {
            url = window.URL.createObjectURL(file) ;// mozilla(firefox)
        }
        else if (window.webkitURL != undefined) {
            url = window.webkitURL.createObjectURL(file) ;// webkit or chrome
        }
        return url ;
    }

    //上传图片到服务器
    function uploadImgToServers(file_input,img) {
        var formData = new FormData();
        formData.append('file',file_input[0].files[0]);
        $.ajax({
            url: '/complain/upFile',
            type: 'POST',
            dataType: 'json',
            contentType: false,
            processData: false,
            data: formData,
            success:function(d){
                if (d.code == 0){
                    img.attr('data_img',d.msg);
                }
            }
        })
    }

    //判断图片上传url是否为空  并返回图片url
    function verify_img_url() {
        var img_1 = $('#complain_by_imgurl_one').attr('data_img');
        var img_2 = $('#complain_by_imgurl_two').attr('data_img');
        var img_3 = $('#complain_by_imgurl_three').attr('data_img');

        if(img_1 == undefined){
            img_1 = ''
        }

        if(img_2 == undefined){
            img_2 = ''
        }

        if(img_3 == undefined){
            img_3 = ''
        }

        return img_1 + ',' + img_2 + ',' + img_3;
    }

    //提交表单
    post_form();
    function post_form() {
        $('#post').on('click',function () {
            form_give_value();
            //$('#myLargeModalLabel').modal()
        });
    }
    
    //form表单赋值
    function form_give_value() {
        var post_form = $('#post_form');//获取form表单
        //投诉人
        post_form.find('input[name="hos_people_name"]').val($('#complain_name').val());//投诉人  姓名
        post_form.find('input[name="hos_people_age"]').val($('#complain_age').val());//投诉人 年龄
        post_form.find('input[name="hos_people_phone"]').val($('#complain_phone').val());//投诉人  手机
        post_form.find('input[name="hos_people_wx"]').val($('#complain_wechat').val());//投诉人  微信号

        //投诉信息
        post_form.find('input[name="hos_by_people_name"]').val($('#complain_by_name').val());//被投诉人 姓名
        post_form.find('input[name="hos_by_people_content"]').val($('#complain_by_content').val());//投诉内容
        post_form.find('input[name="hos_by_people_imgurl"]').val(verify_img_url());//图片上传

        console.log(verify_img_url());

        post_form_ajax(post_form.serialize());
    }

    //提交表单数据  ajax
    function post_form_ajax(data) {
        $.ajax({
            url:'/complain/subinHospitalCom',
            data: {
                postData: data
            },
            type: 'POST',
            dataType: 'json',
            success:function (data) {
                $('#myLargeModalLabel').modal()
                console.log(data);
            },
            error: function (err) {
                console.log(err);
            }
        })
    }
});